package com.hawk.eye.framework.utils;

import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author zd
 */
public class CollectionUtil extends CollectionUtils {

    public static boolean isNotEmpty(@Nullable Collection<?> collection) {
        return !isEmpty(collection);
    }

    public static boolean isNotEmpty(@Nullable Map<?, ?> map) {
        return !isEmpty(map);
    }

    public static <E> List<E> toList(Iterable<E> elements) {
        Objects.requireNonNull(elements, "elements es is null.");
        if (elements instanceof Collection) {
            return new ArrayList((Collection) elements);
        } else {
            Iterator<E> iterator = elements.iterator();
            ArrayList list = new ArrayList();

            while (iterator.hasNext()) {
                list.add(iterator.next());
            }

            return list;
        }
    }

    public static <E> List<E> toList(E[] array) {
        return Arrays.stream(array)
                .collect(Collectors.toList());
    }

    public static <T> List<T> filter(List<T> target, Predicate<? super T> filter) {
        Objects.requireNonNull(target);
        return stream(target).filter(filter).collect(Collectors.toList());
    }

    private static <T> Stream<T> stream(List<T> target) {
        return target.stream();
    }

}
